package cj.com.chabaike.ui.frag;


import android.app.Dialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.wangjie.androidbucket.utils.ABTextUtil;
import com.wangjie.androidbucket.utils.imageprocess.ABShape;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionButton;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionHelper;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionLayout;
import com.wangjie.rapidfloatingactionbutton.contentimpl.labellist.RFACLabelItem;
import com.wangjie.rapidfloatingactionbutton.contentimpl.labellist.RapidFloatingActionContentLabelList;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cj.com.chabaike.R;
import cj.com.chabaike.bll.PandaPagerManager;
import cj.com.chabaike.entity.EliteList;
import cj.com.chabaike.entity.NewsList;
import cj.com.chabaike.entity.seach.Seach_ContentList;
import cj.com.chabaike.entity.special.Special_detail_data_list_conlist;
import cj.com.chabaike.ui.act.LonginAndJoin.CommentPager;
import cj.com.chabaike.ui.act.MainPager;
import cj.com.chabaike.ui.utils.Navigator;
import cj.com.chabaike.ui.utils.TransitionHelper;

public class ThingDetailFragment extends TransitionHelper.BaseFragment implements RapidFloatingActionContentLabelList.OnRapidFloatingActionContentLabelListListener {

    @InjectView(R.id.detail_title)
    TextView titleTextView;
    @InjectView(R.id.detail_body)
    TextView detailBodyTextView;
    @InjectView(R.id.detail_over_view)
    FrameLayout rootLayout;
    @InjectView(R.id.detail_WebView)
    WebView webView;
    @InjectView(R.id.tempLayout)
    LinearLayout linearLayout;

    PandaPagerManager pandaPagerManager;
    EliteList eliteList;
    NewsList news;
    Seach_ContentList seach_contentList;
    Special_detail_data_list_conlist special_detail_data_list_conlist;
    String title = "丢失";
    String id = "0000";

    @InjectView(R.id.rfal_layout)
    RapidFloatingActionLayout rfaLayout;
    @InjectView(R.id.rfab_btn)
    RapidFloatingActionButton rfabBtn;

    private RapidFloatingActionHelper rfabHelper;
    private PopupWindow popupWindow;

    String TAG = "eeeee";

    public static ThingDetailFragment create() {
        ThingDetailFragment f = new ThingDetailFragment();
        return f;
    }

    public ThingDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pandaPagerManager = PandaPagerManager.getPandaPagerManagerInstance();


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_thing_detail, container, false);
        ButterKnife.inject(this, rootView);
        //String itemText = getActivity().getIntent().getStringExtra("item_text");




        initFloatBtn();
        String itemText = getActivity().getIntent().getStringExtra("item_text");
        eliteList = (EliteList) getActivity().getIntent().getSerializableExtra("EliteList");
        news = (NewsList) getActivity().getIntent().getSerializableExtra("NewsList");
        seach_contentList = (Seach_ContentList) getActivity().getIntent().getSerializableExtra("Seach_ContentList");
        special_detail_data_list_conlist = (Special_detail_data_list_conlist) getActivity().getIntent().getSerializableExtra("Special_detail_data_list_conlist");

        ;
        if (eliteList != null) {
            title = eliteList.getTitle();
            id = eliteList.getId();
        }
        if (news != null) {
            title = news.getTitle();
            id = news.getID();
        }
        if (seach_contentList != null) {
            title = seach_contentList.getTitle();
            id = seach_contentList.getID();
        }
        if(special_detail_data_list_conlist!=null){
            title=special_detail_data_list_conlist.getTitle();
            id=special_detail_data_list_conlist.getID();

        }

        //  titleTextView.setText(itemText);
        titleTextView.setText(title);

        webView.getSettings().setJavaScriptEnabled(true);
        Log.w("载入的url", " >>>>>  " + pandaPagerManager.getDetailUrl(title));
        webView.loadUrl(pandaPagerManager.getDetailUrl(id));
        initDetailBody();
        return rootView;


    }

    @Override
    public void onResume() {
        super.onResume();
      //  MainPager.of(getActivity()).openOrCloseDrawerToggle(false);
    }

    @Override
    public void onStop() {
        super.onStop();
       // MainPager.of(getActivity()).openOrCloseDrawerToggle(true);
    }

    private void initFloatBtn() {
        RapidFloatingActionContentLabelList rfaContent = new RapidFloatingActionContentLabelList(getActivity().getApplicationContext());
        rfaContent.setOnRapidFloatingActionContentLabelListListener(this);
        List<RFACLabelItem> items = new ArrayList<>();
        items.add(new RFACLabelItem<Integer>()
                        .setLabel("发表评论")
                        .setLabelColor(Color.WHITE)
                        .setResId(R.mipmap.ico_test_b)
                        .setIconNormalColor(0xff056f00)
                        .setIconPressedColor(0xff0d5302)
                        .setLabelBackgroundDrawable(ABShape.generateCornerShapeDrawable(0xaa000000, ABTextUtil.dip2px(getActivity(), 4)))
                        .setWrapper(0)
//                        .setLabelBackgroundDrawable();
        );
        items.add(new RFACLabelItem<Integer>()
                        .setLabel("查看评论")
                        .setResId(R.drawable.icon_comment_show)
                        .setIconNormalColor(0xff283593)
                        .setLabelColor(Color.WHITE)
                        .setIconPressedColor(0xff1a237e)
                        .setLabelBackgroundDrawable(ABShape.generateCornerShapeDrawable(0xaa000000, ABTextUtil.dip2px(getActivity(), 4)))
                        .setWrapper(1)
        );
        items.add(new RFACLabelItem<Integer>()
                        .setLabel("收藏")
                        .setResId(R.mipmap.ico_test_c)
                        .setIconNormalColor(0xff4e342e)
                        .setIconPressedColor(0xff3e2723)
                        .setLabelColor(Color.WHITE)
                        .setLabelSizeSp(14)
                        .setLabelBackgroundDrawable(ABShape.generateCornerShapeDrawable(0xaa000000, ABTextUtil.dip2px(getActivity(), 4)))
                        .setWrapper(2)
        );
        items.add(new RFACLabelItem<Integer>()
                        .setLabel("字体")
                        .setLabelColor(Color.WHITE)
                        .setResId(R.drawable.icon_text_size)
                        .setIconNormalColor(0xfcfffc5f)
                        .setIconPressedColor(0xfcfcff1f)
                        .setLabelBackgroundDrawable(ABShape.generateCornerShapeDrawable(0xaa000000, ABTextUtil.dip2px(getActivity(), 4)))
                        .setWrapper(2)
        );
        items.add(new RFACLabelItem<Integer>()
                        .setLabel("分享")
                        .setLabelColor(Color.WHITE)
                        .setResId(R.drawable.icon_share)
                        .setIconNormalColor(0xffff5e54)
                        .setIconPressedColor(0xffff3e2b)
                        .setLabelBackgroundDrawable(ABShape.generateCornerShapeDrawable(0xaa000000, ABTextUtil.dip2px(getActivity(), 4)))
                        .setWrapper(3)
        );

        rfaContent
                .setItems(items)
                .setIconShadowRadius(ABTextUtil.dip2px(getActivity(), 5))
                .setIconShadowColor(0xff888888)
                .setIconShadowDy(ABTextUtil.dip2px(getActivity(), 5));


        rfabHelper = new RapidFloatingActionHelper(
                getActivity().getApplicationContext(),
                rfaLayout,
                rfabBtn,
                rfaContent
        ).build();

    }

    //圆形悬浮button标题点击事件
    @Override
    public void onRFACItemLabelClick(int position, RFACLabelItem rfacLabelItem) {
        switch (rfacLabelItem.getLabel()) {
            case "发表评论":
                showPopupWindow();
                break;
            case "分享":
                showShareWindow();
                break;
            case "收藏":
                Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_SHORT).show();
                break;
            case "查看评论":
                Intent intent = new Intent(getActivity(), CommentPager.class);
                startActivity(intent);
                break;
            case "字体":
                FontDialog();
                break;
            default:
                break;

        }
        rfabHelper.toggleContent();

    }

    //圆形悬浮button图标点击事件
    @Override
    public void onRFACItemIconClick(int i, RFACLabelItem rfacLabelItem) {
        switch (rfacLabelItem.getResId()) {
            case R.mipmap.ico_test_b:
                showPopupWindow();
                break;
            case R.drawable.icon_share:
                showShareWindow();
                break;
            case R.mipmap.ico_test_c:
                Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_SHORT).show();
                break;
            case R.drawable.icon_comment_show:
                Intent intent = new Intent();
                intent.setAction("CommentPager");
                getActivity().startActivity(intent);
                break;
            case R.drawable.icon_text_size:
                FontDialog();
                break;
            default:
                break;
        }
        rfabHelper.toggleContent();
    }

    private void FontDialog() {


        Dialog dialog = new AlertDialog.Builder(getActivity()).setTitle("字体大小")
                .setPositiveButton("大", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        webView.getSettings().setTextSize(WebSettings.TextSize.LARGER);
                    }
                }).setNeutralButton("小", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        webView.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
                    }
                }).setNegativeButton("中", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
                    }
                }).create();
         dialog.show();




/*
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        builder.setIcon()
        builder.setTitle("字体");
        builder.setPositiveButton("大", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                webView.getSettings().setTextZoom(25);
                Toast.makeText(getActivity(),"大",Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("中", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                webView.getSettings().setTextZoom(18);
                Toast.makeText(getActivity(), "中", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("小", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                webView.getSettings().setTextZoom(11);
                Toast.makeText(getActivity(), "小", Toast.LENGTH_SHORT).show();
            }

        });
        builder.create().show();
        */
    }

    /**
     * 显示popupWindow
     */


    private void showPopupWindow() {

        // 利用layoutInflater获得View
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow_layout, null);

        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()

        final PopupWindow window = new PopupWindow(view,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        window.setFocusable(true);


        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);


        // 设置popWindow的显示和消失动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 在底部显示
        window.showAtLocation(getActivity().findViewById(R.id.rfab_btn),
                Gravity.BOTTOM, 0, 0);
        // 这里检验popWindow里的button是否可以点击
        Button btnOK = (Button) view.findViewById(R.id.certain_btn);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out.println("第一个按钮被点击了");
            }
        });
        Button cancel_btn = (Button) view.findViewById(R.id.cancel_btn);

        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });

        //popWindow消失监听方法
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                System.out.println("popWindow消失");
            }
        });


    }

    private void showShareWindow() {
        // 利用layoutInflater获得View
        View view2 = LayoutInflater.from(getActivity()).inflate(R.layout.share_layout, null);

        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()

        final PopupWindow window2 = new PopupWindow(view2,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        window2.setFocusable(true);


        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw2 = new ColorDrawable(0xb0000000);
        window2.setBackgroundDrawable(dw2);


        // 设置popWindow的显示和消失动画
        window2.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 在底部显示
        window2.showAtLocation(getActivity().findViewById(R.id.rfab_btn),
                Gravity.BOTTOM, 0, 0);
        // 这里检验popWindow里的button是否可以点击
        LinearLayout layout_btn_one = (LinearLayout) view2.findViewById(R.id.layout_one);
        layout_btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "此功能尚未完善，请关注APP更新", Toast.LENGTH_SHORT).show();
            }
        });
        LinearLayout layout_btn_two = (LinearLayout) view2.findViewById(R.id.layout_two);
        layout_btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "此功能尚未完善，请关注APP更新", Toast.LENGTH_SHORT).show();
            }
        });
        Button btn_QX = (Button) view2.findViewById(R.id.quxiao_btn);
        btn_QX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window2.dismiss();
            }
        });
        //popWindow消失监听方法
        window2.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                System.out.println("popWindow消失");
            }
        });
    }


    private void initDetailBody() {
        linearLayout.setAlpha(50);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                linearLayout.animate().alpha(0).start();
                linearLayout.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        }, 500);


    }

    @Override
    public void onBeforeViewShows(View contentView) {
        ViewCompat.setTransitionName(rootLayout, "detail_element");
        // ViewCompat.setTransitionName(getActivity().findViewById(R.id.fab), "fab");
        //  BaseActivity.of(getActivity()).fab.setTranslationY(400);

        //  TransitionHelper.excludeEnterTarget(getActivity(), R.id.toolbar_container, true);
        TransitionHelper.excludeEnterTarget(getActivity(), R.id.mainlayout, true);
    }

    @Override
    public void onBeforeEnter(View contentView) {
        MainPager.of(getActivity()).fragmentBackround.animate().scaleX(.92f).scaleY(.92f).alpha(.6f).setDuration(Navigator.ANIM_DURATION).setInterpolator(new AccelerateInterpolator()).start();
        //  BaseActivity.of(getActivity()).setHomeIcon(MaterialMenuDrawable.IconState.BURGER);
        //  BaseActivity.of(getActivity()).animateHomeIcon(MaterialMenuDrawable.IconState.ARROW);
    }

    @Override
    public boolean onBeforeBack() {
        //   MainPager.of(getActivity()).animateHomeIcon(MaterialMenuDrawable.IconState.BURGER);
        MainPager.of(getActivity()).fragmentBackround.animate().scaleX(1).scaleY(1).alpha(1).translationY(0).setDuration(Navigator.ANIM_DURATION / 4).setInterpolator(new DecelerateInterpolator()).start();
        TransitionHelper.fadeThenFinish(detailBodyTextView, getActivity());
        return false;
    }
}
